import store from '@/store'

const getTokenFromLocal = () => localStorage.getItem("token");


const getTokenFromVuex = () => {

  let token = store.getters.token;
  if (!token) {
    token = getTokenFromLocal();    
    if (token) {
      store.dispatch('putToken', token);
      return token;
    }
    return token;
  }

  return token;
};

export const getToken = () => getTokenFromVuex() || null;

export const putToken = (token) => {
  localStorage.setItem("token", token);
  store.dispatch('putToken', token);

};

export const removeToken = () => {
  store.dispatch('putToken', "");
  localStorage.removeItem("token");
};

